/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import listo.utils.FileUtils2;
import listo.utils.MiscUtils;
import listo.utils.logging.Log;
import listo.utils.reflection.Reflector;

import java.io.File;
import java.util.Properties;

@Singleton
public class WindowState {

    public enum CompletionView {
        all, todays, none
    }

    public enum TaskDialogState {
        quick, standard, both
    }

    private final String filename;
    private final Reflector reflector;
    private final Log log;

    private int windowPosX = -1;
    private int windowPosY = -1;
    private int windowSizeX;
    private int windowSizeY;
    private int windowSplitX;
    private CompletionView completionView;
    private TaskDialogState taskDialogState;

    private int taskDialogWidth;

    @Inject
    public WindowState(Log log, Reflector reflector, AppConfig appConfig, Preferences preferences) {
        this.log = log;
        File file = new File(preferences.getStateFile());
        if (file.exists()) {
            log.debug("State file %1$s found, loading window state", preferences.getStateFile());
            String text = FileUtils2.readAllText(file);
            Properties properties = MiscUtils.deserialize(text);
            if (properties != null) {
                try {
                    reflector.f(this).loadFromProperties(properties);
                }
                catch (Exception e) {
                    log.error("Could not load window state file: %1$s", e);
                }
            }
        } else {
            log.debug("State file %1$s not found, using default window state", preferences.getStateFile());
        }

        this.filename = preferences.getStateFile();
        this.reflector = reflector;

        if (windowPosX < 0) windowPosX = appConfig.getWindowDefaultPosX();
        if (windowPosY < 0) windowPosY = appConfig.getWindowDefaultPosY();
        if (windowSizeX < appConfig.getWindowMinSizeX()) windowSizeX = appConfig.getWindowDefaultSizeX();
        if (windowSizeY < appConfig.getWindowMinSizeY()) windowSizeY = appConfig.getWindowDefaultSizeY();
        if (windowSplitX < appConfig.getWindowMinSplitX()) windowSplitX = appConfig.getWindowDefaultSplitX();
        if (completionView == null) completionView = appConfig.getDefaultCompletionView();
        if (taskDialogState == null) taskDialogState = appConfig.getDefaultTaskDialogState();
        if (taskDialogWidth < appConfig.getTaskDialogMinWidth()) taskDialogWidth = appConfig.getTaskDialogMinWidth();
    }

    public void save() {
        log.debug("Saving window state to file %1$s", filename);
        Properties properties = reflector.f(this).saveToProperties(false);
        String text = MiscUtils.serialize(properties, "listo window state");
        FileUtils2.writeAllText(text, filename);
    }

    public int getWindowPosX() {
        return windowPosX;
    }

    public void setWindowPosX(int windowPosX) {
        this.windowPosX = windowPosX;
    }

    public int getWindowPosY() {
        return windowPosY;
    }

    public void setWindowPosY(int windowPosY) {
        this.windowPosY = windowPosY;
    }

    public int getWindowSizeX() {
        return windowSizeX;
    }

    public void setWindowSizeX(int windowSizeX) {
        this.windowSizeX = windowSizeX;
    }

    public int getWindowSizeY() {
        return windowSizeY;
    }

    public void setWindowSizeY(int windowSizeY) {
        this.windowSizeY = windowSizeY;
    }

    public int getWindowSplitX() {
        return windowSplitX;
    }

    public void setWindowSplitX(int windowSplitX) {
        this.windowSplitX = windowSplitX;
    }

    public CompletionView getCompletionView() {
        return completionView;
    }

    public void setCompletionView(CompletionView completionView) {
        this.completionView = completionView;
    }

    public TaskDialogState getTaskDialogState() {
        return taskDialogState;
    }

    public void setTaskDialogState(TaskDialogState taskDialogState) {
        this.taskDialogState = taskDialogState;
    }

    public int getTaskDialogWidth() {
        return taskDialogWidth;
    }

    public void setTaskDialogWidth(int taskDialogWidth) {
        this.taskDialogWidth = taskDialogWidth;
    }

}